#ifndef FILE_DIALOG_FEATURE_H
#define FILE_DIALOG_FEATURE_H

#include <QFileInfo>

#include "../../model/player.h"
#include "../../model/core_io.h"

#include "feature.h"
#include "../widgets/file_dialog.h"

class OpenFileDialogFeature: public Feature, public FileDialogListener
{
public:
    OpenFileDialogFeature(int _id, Engine* _engine, int _x, int _y, int _w, int _h, ControllerProxy* _cProxy, MainWidget* _mw)
        :Feature(_id, _engine, _mw, _cProxy, _x, _y, _w, _h)
    {
        fd_open = new FileDialog(this, engine, 15, 15, w - 30, h - 30, _mw, FileDialog::Open, "E:/");
        fd_open->AddFileDialogListener(this);
        AddChild(fd_open);
    }

    virtual ~OpenFileDialogFeature(){}

    virtual void OnSave(FileDialog* sender, QString absolutePath, QString chosenName){}

    virtual void OnOpen(FileDialog* sender, QString filename)
    {
        if( cProxy->openSong )
        {
            Player::GetInstance()->Open( filename );
            CoreIO::docsManager->updateRecentSongs( filename );
            cProxy->stringParam = filename;

            QFileInfo fi( filename );
            if( fi.suffix() == "mps" )
            {
                cProxy->UnloadRequest( ControllerProxy::PLAYER_FEATURE );
            }
            else
            {
                cProxy->UnloadRequest( ControllerProxy::EDIT_SONG_FEATURE );
            }
        }else
        {
            // opening playlist
        }
    }

    virtual void SetNameFilters(QStringList& filters)
    {
        fd_open->SetNameFilters(filters);
    }

    virtual void SetDir(const QString& newDir)
    {
        fd_open->SetDir(newDir);
    }

private:
    FileDialog* fd_open;
};

class SaveFileDialogFeature: public Feature, public FileDialogListener
{
public:
    SaveFileDialogFeature(int _id, Engine* _engine, int _x, int _y, int _w, int _h, ControllerProxy* _cProxy, MainWidget* _mw)
        :Feature(_id, _engine, _mw, _cProxy, _x, _y, _w, _h)
    {
        fd_save = new FileDialog(this, engine, 15, 15, w - 30, h - 30, _mw, FileDialog::Save, ".");
        fd_save->AddFileDialogListener(this);
        AddChild(fd_save);
    }

    virtual ~SaveFileDialogFeature(){}

    virtual void OnSave(FileDialog* sender, QString absolutePath, QString chosenName)
    {
        if(cProxy->saveSong)
        {
            QString completeName = absolutePath + chosenName;
            QFileInfo fileInfo(completeName);
            if(fileInfo.suffix() != "mps")
            {
                completeName += ".mps";
            }
            CoreIO::SaveSong( completeName, Player::GetInstance()->song );
            //cProxy->UnloadRequest(cProxy->prevFeatureId);
        }else
        {
            // saving playlist;
        }
    }

    virtual void OnOpen(FileDialog* sender, QString filename){}

private:
    FileDialog* fd_save;
};

#endif // FILE_DIALOG_FEATURE_H
